package com.bayi.storage.utils

import java.net.URI
import java.util.regex.Pattern

object NumberUtils {

    /**
     * 判断奇偶数
     */
    fun getOddAndEven(num: Int): Boolean {
        return num % 2 == 0
    }

    /**
     * 验证IP是否正确
     */
    fun isMatches(ip: String?): Boolean {
        val flag = false
        try {
            val regex =
                "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$"
            //正确的是
            val p = Pattern.compile(regex)
            val m = p.matcher(ip!!)
            if (m.find()) {
                return true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return flag
    }

    /**
     * 验证IP是否正确
     */
    fun isMatchesHttp(http: String?): Boolean {
        var flag = false
        try {
            val webSocketUri = URI(http)
            if (webSocketUri.host != null) {
                flag = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return flag
    }
}